
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
  <meta name="description" content="xms">
  <meta name="author" content="Xms">
  <link rel="icon" href="/content/favicon.ico">

  <title>组织 - 安全 - 安全角色 - 字段授权</title>

  <!-- Bootstrap core CSS -->
  <link href="/content/css/bootstrap3.3.5/bootstrap.min.css" rel="stylesheet">

  <!-- Custom styles for this template -->
  <link href="/content/css/font-awesome.min.css" rel="stylesheet">
  <link href="/content/css/common.css" rel="stylesheet">
  <link href="/content/js/jquery-toast/jquery.toast.min.css" rel="stylesheet">
  <link id="themeLink" href="/content/css/theme/default.css" rel="stylesheet" />
  <!-- Bootstrap core JavaScript ================================================== -->
  <!-- Placed at the end of the document so the pages load faster -->
  <script src="/content/js/jquery.min.js"></script>
  <script src="/content/js/bootstrap.min.js"></script>
  <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
  <script src="/content/js/ie10-viewport-bug-workaround.js"></script>
  <script src="/content/js/jquery.bootstrap.min.js"></script>
  <script src="/content/js/json2.js"></script>
  <script src="/content/js/xms.utility.js"></script>
  <script src="/content/js/jquery-ui-1.10.3/ui/jquery.ui.core.js"></script>
  <script src="/content/js/jquery-ui-1.10.3/ui/jquery.ui.widget.js"></script>
  <script src="/content/js/jquery-ui-1.10.3/ui/jquery.ui.mouse.js"></script>
  <script src="/content/js/jquery-ui-1.10.3/ui/jquery.ui.draggable.js"></script>
  <script src="/content/js/jquery-toast/jquery.toast.min.js"></script>
  <script src="/content/js/xms.jquery.js"></script>
  <script src="/content/js/xms.web.js"></script>
  <script src="/content/js/commonlabels.js"></script>
  <script src="/content/js/common/pagecontext.js"></script>
  <script src="/api/user/usercontext"></script>
  <script>
  jQuery(function () {
    Xms.Web.Loading();
    if (typeof parent !== 'undefined') {
      if (parent != window) {//判断当前页面是否为子页面
        $('body').addClass('xms-subpage');
      }
    }
  });
  function changeTheme(body,themename) {
    $("#themeLink").attr('href', '/content/css/theme/' + themename+'.css');
  }
</script>
  <link href="/content/customize/layui/css/layui.css" rel="stylesheet">
  <style>
    .radio-block {
      padding: 5px;
      color: white;
      cursor: pointer;
    }
  </style>
  <style>
    body {
      padding-top: 0;
    }
  </style>
</head>

<body data-spy="scroll" data-target="#myScrollspy">
<div class="container-fluid" id="body">
  <div class="row">
    <div class="shadeBox" style="background-color:#000000; opacity:0.5;width:100%;position:absolute;z-index:10;display:none;"></div>
    <div class="col-md-12" id="main" style="margin-bottom:80px;">
      <div><br class="breadcrumb" /></div>
      <div id="content"></div>
      <div class="page-render-wrap">
        <!--main-->

        <div class="container-fluid">
          <div class="row" style="padding:5px 0px;">
            <div class="col-sm-11 btn-group" id="owners">
              <button type="button" class="btn btn-sm btn-default">类型：</button>
            </div>
          </div>
        </div>
        <div class="panel panel-default">
          <div class="panel-heading">
            <h3 class="panel-title">
              <a data-toggle="collapse"
                 href="#collapseTwo">
                <strong id="rolename" class="text-primary"></strong> - <strong>字段授权</strong>
              </a>
            </h3>
          </div>
          <div id="collapseTwo" class="panel-collapse collapse in">
            <div class="panel-body">
              <form id="editform" class="form-horizontal" data-jsonajax="true" data-istip="true"
                    action="/xms/api/security/roleobjectaccess/save" method="post">
                <input id="RoleId" name="RoleId" type="hidden" />
                <input id="ResourceName" name="ResourceName" type="hidden" />
                <div class="form-group col-sm-12">
                  <div class="table-responsive">
                    <table class="table table-bordered table-condensed" id="fields">
                      <thead class="table-header">
                      <tr>
                        <th>实体</th>
                        <th>字段</th>
                        <th>权限</th>
                      </tr>
                      </thead>
                      <tbody></tbody>
                    </table>
                  </div>
                </div>
              </form>
            </div>
          </div>
        </div>

        <nav class="navbar navbar-default navbar-fixed-bottom" role="navigation" id="body-footer">
          <div class="container">
            <div class="navbar-form navbar-right" id="body-footer-content">
              <button type="button" id="save_button" class="btn btn-primary"><span
                      class="glyphicon glyphicon-saved"></span> 保存
              </button>
            </div>
          </div>
        </nav>
      </div>
    </div>
  </div>
</div>

<script src="/content/js/xms.metadata.js"></script>
<script src="/content/js/xms.utility.js"></script>
<script src="/content/js//jquery-validate/jquery.validate.min.js"></script>
<script src="/content/js/jquery.form.js"></script>
<script src="/content/js/xms.web.js"></script>
<script src="/content/js/xms.jquery.js"></script>
<script src="/content/js/knockout-2.2.0.js"></script>
<script>
  var model = $.urlParamObj(location.href);
  $(function () {
    $('#RoleId').val(model.roleid);
    $('#ResourceName').val(model.resourcename);
    bindObject(function (items) {
      bindFields(function () {
        $(items).each(function (i, n) {
          var $item = $('#objectidtd_' + n.objectid);

          if ($item.length > 0 && (n.accessrightsmask || n.accessrightsmask === 0)) {
            $item.find('input[value=' + n.accessrightsmask + ']').prop('checked', true).trigger('change');
            $item.parents('tr:first').find('input[data-name="mask"]').val(n.accessrightsmask);
          }
        });
      });
    });

    getRoleInfo();
    loadResourceOwners();
    $('#save_button').on('click', function(){$("#editform").submit();});
    $("#editform").submit(function () {
      var data = {};
      data.roleid = model.roleid;
      data.resourcename = model.resourcename;
      data.items = [];
      $('#fields>tbody>tr').each(function(i, n){
        var $objectid_input = $(n).find('input[data-name=objectid]');
        var $mask_input = $(n).find('input[data-name=mask]');
        if($mask_input.val() > 0) {
          data.items.push({objectid: $objectid_input.val(), mask: $mask_input.val()});
        }
      });
      console.log('post data', data);
      Xms.Web.Post('/api/security/roleobjectaccess/save', data, false, function(response){
        Xms.Web.Alert(response.IsSuccess, response.Content || response.ErrorMessage);
      }, null, true, false);
      return false;
    });
  });

  function getRoleInfo(){
    Xms.Web.GetJson('/api/security/role/getbyid/' + model.roleid, null, function(data){
      if (!data.IsSuccess || !data.Content) {
        Xms.Web.FailureHandler(data);
        return;
      }
      data = data.Content;
      $('#rolename').text(data.name);
    });
  }

  function loadResourceOwners() {
    Xms.Web.GetJson(ORG_SERVERURL + "/api/security/resourceowners", null, function (data) {
      if (!data.IsSuccess) return;
      var items = data.Content;
      var resourceName = $('#ResourceName').val().toLowerCase();
      var $container = $('#owners');
      $(items).each(function (i, n) {
        if(!n.statecode) return true;
        var url = (n.uiendpoint || '/security/editrolepermission') + '?roleid=' + $("#RoleId").val() + '&resourcename=' + n.modulename;
        $container.append($('<button type="button" class="btn btn-sm ' + (resourceName == n.modulename.toLowerCase() ? 'btn-info' : 'btn-default') + '" data-url="' + url + '">' + (n.modulelocalizedname || n.modulename) + '</button>'));
      });
      $container.find('button').on('click', null, function () {
        Xms.Web.Redirect($(this).attr('data-url'));
      });
    });
  }

  function bindFields(callback) {
    Xms.Ajax.GetJson('/api/schema/attribute/privilegeresource?authorizationenabled=true', null, function (d) {
      console.log(d);
      var items = d.Content;
      var $container = $('#fields>tbody');
      var step = 0;
      $(items).each(function (i, n) {
        var $item = $('<tr></tr>');
        //  n.children.push(n.children[0])
        var childCount = n.children.length;
        $item.append($('<td rowspan="' + childCount + '">' + n.label + '</td>'));

        if (childCount > 1) $container.append($item);

        $(n.children).each(function (j, f) {
          var $item2 = j > 0 ? $('<tr id="objectid_' + f.id + '"></tr>') : $item;
          $item2.append($('<td>' + f.label + '<input type="hidden" data-name="objectid" name="objectid['+step+']" value="' + f.id + '" /></td>'));
          $item2.append($('<td id="objectidtd_' + f.id + '"><input type="hidden" data-name="mask" name="mask['+step+']" value="0" /></td>'));
          $container.append($item2);
          $item2.find('input[name^="mask"]').picklist({
            items: [{ value: 0, text: '无' }, { value: 1, text: '仅查看' }, { value: 3, text: '可编辑' }],
            displaytype: 'radio',
            itemTmpl: function (self, _thisid, name, v, selected, text) {
              v = v || 0;
              if (v == 0 && selected=='') {
                selected = 'checked';
              }
              return '<label class="squart-radio"><input type="radio" class="squart" data-sourceid="' + (self.attr("id") || _thisid) + '" name="' + (name + +i+'_'+j) + '" value="' + v + '"' + selected + '>' + text + '</label>'
            }
          })
          step++;
        });

      });

      callback && callback();
    });
  }

  function bindObject(callback) {
    Xms.Ajax.GetJson('/api/security/roleobjectaccess/list?roleid=' + $('#RoleId').val() + '&resourcename=' + $('#ResourceName').val(), null, function (d) {
      console.log(d);
      var items = d.Content;

      callback && callback(items);
    });
  }
</script>

</body>
</html>